package com.example.weather

import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.RequestQueue
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.weather.weather.Forecast
import com.example.weather.weather.Weather
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class MainActivity2 : AppCompatActivity() {

    private val baseURL = "http://t.weather.itboy.net/api/weather/city/"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)
        // 获取城市id
        val cityCode: String? = intent.getStringExtra("city.code")

        // 获取网络
        val queue = Volley.newRequestQueue(this)
        // 构造请求
        val stringRequest = StringRequest(baseURL + cityCode, {
            val gson = Gson()
            val weatherType = object : TypeToken<Weather>() {}.type // 使用正确的 TypeToken
            val weather: Weather = gson.fromJson(it, weatherType)
            runOnUiThread {
                textView_city.text = weather.cityInfo.city
                textView_province.text = weather.cityInfo.parent
                textView_shidu.text = weather.data.shidu
                textView_wendu.text = weather.data.wendu
                if (weather.data.forecast.isNotEmpty()) { // 确保列表不为空
                    val firstDay = weather.data.forecast.first() // 获取第一个 Forecast
                    when (firstDay.type) {
                        "晴" -> imageView.setImageResource(R.drawable.sun)
                        "阴" -> imageView.setImageResource(R.drawable.cloud)
                        "多云" -> imageView.setImageResource(R.drawable.mcloud)
                        "阵雨" -> imageView.setImageResource(R.drawable.rain)
                        else -> imageView.setImageResource(R.drawable.thunder)
                    }
                }
            }

            val listView = findViewById<ListView>(R.id.listView4)

            val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, weather.data.forecast)
            listView.adapter = adapter

            Log.d("MainActivity2", " ${weather.cityInfo.city} ${weather.cityInfo.parent} ")
        }, {
            Log.d("MainActivity2", "$it")
        })
        queue.add(stringRequest)

        // 初始化视图
        textView_city = findViewById(R.id.textView_city)
        textView_province = findViewById(R.id.textView_province)
        textView_shidu = findViewById(R.id.textView_shidu)
        textView_wendu = findViewById(R.id.textView_wendu)
        imageView = findViewById(R.id.imageView)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private lateinit var textView_city: TextView
    private lateinit var textView_province: TextView
    private lateinit var textView_shidu: TextView
    private lateinit var textView_wendu: TextView
    private lateinit var imageView: ImageView
}